package com.gksc.video.controller;

import com.gksc.video.data.DeviceInfo;
import com.gksc.video.data.JsonResponse;
import com.gksc.video.data.OpResInfo;
import com.gksc.video.manager.VideoManager;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.Arrays;
import java.util.List;

/**
 * @className: VideoController
 * @description: 视频服务接口
 * @author: Liao.yx
 * @email: 1229053515@qq.com
 * @create: 2021年05月15日 10时36分
 * @copyRight: 2020 liaoyongxiang All rights reserved.
 **/
@Slf4j
@RestController
@Api("视频服务API")
@RequestMapping("/video")
@CrossOrigin(allowCredentials = "true")
public class VideoController {
    @Autowired
    private VideoManager videoManager;

    @PostMapping(value = "/openVideo", produces = "application/json;charset=UTF-8")
    @ApiOperation(value = "打开视频", notes = "")
    public JsonResponse openVideo(@RequestBody DeviceInfo device, Integer type) {
        try {
            if (ObjectUtils.isEmpty(device)) {
                return JsonResponse.error("参数不能为空");
            }

            OpResInfo<DeviceInfo> opRes = videoManager.openVideo(device, type);
            if (opRes.isOpRes()) {
                return JsonResponse.success(opRes.getData());
            } else {
                return JsonResponse.error(opRes.getErrorDesc());
            }
        } catch (Exception e) {
            e.printStackTrace();
            log.error("系统异常，异常原因是", e);
            return JsonResponse.error("系统异常");
        }
    }

    @PostMapping(value = "/closeVideo", produces = "application/json;charset=UTF-8")
    @ApiOperation(value = "关闭视频", notes = "")
    public JsonResponse closeVideo(String tokens) {
        try {
            if (ObjectUtils.isEmpty(tokens)) {
                return JsonResponse.error("参数不能为空");
            }

            OpResInfo<List<DeviceInfo>> opRes = videoManager.closeVideo(Arrays.asList(tokens.split(",")));
            if (opRes.isOpRes()) {
                return JsonResponse.success(opRes.getData());
            } else {
                return JsonResponse.error(opRes.getErrorDesc());
            }
        } catch (Exception e) {
            e.printStackTrace();
            log.error("系统异常，异常原因是", e);
            return JsonResponse.error("系统异常");
        }
    }

    @PostMapping(value = "/keepAlive", produces = "application/json;charset=UTF-8")
    @ApiOperation(value = "视频流保活", notes = "")
    public JsonResponse keepAlive(String tokens) {
        try {
            if (ObjectUtils.isEmpty(tokens)) {
                return JsonResponse.error("参数不能为空");
            }

            OpResInfo<Boolean> opRes = videoManager.keepAlive(Arrays.asList(tokens.split(",")));
            if (opRes.isOpRes()) {
                return JsonResponse.success(opRes.getData());
            } else {
                return JsonResponse.error(opRes.getErrorDesc());
            }
        } catch (Exception e) {
            e.printStackTrace();
            log.error("系统异常，异常原因是", e);
            return JsonResponse.error("系统异常");
        }
    }
}
